/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api
{	
	import com.flashphoner.api.management.SoundControl;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * Call object implementation
	 ***/	 	
	[Bindable]
	public class Call
	{	
		
		public static const STATE_RING:String = "RING";
		public static const STATE_HOLD:String = "HOLD";		
		public static const STATE_TALK:String = "TALK";
		public static const STATE_FINISH:String = "FINISH";
		public static const STATE_BUSY:String = "BUSY";
		public static const STATE_SESSION_PROGRESS:String = "SESSION_PROGRESS";
		
		public var id:String;
		public var state:String;	
		
		public var iHolded:Boolean = false;
		
		public var sip_state:String;			
		public var callee:String;
		public var caller:String;		
		public var visibleNameCallee:String;
		public var visibleNameCaller:String;
		
		public var playerVideoFormat:String;
		public var streamerVideoFormat:String;
		public var timeOfCall:int = 0;
		private var timer:Timer;

		public var anotherSideUser:String;
		
		public var incoming:Boolean = false;
		public var isVideoCall:Boolean = false;
		
		private var callServerProxy:CallServerProxy;
		
		internal var flash_API:Flash_API;
		
		public function Call(flash_API:Flash_API)
		{
			this.flash_API = flash_API;
			callServerProxy = new CallServerProxy(this,flash_API.phoneServerProxy.nc);
		}
		
		public function hangup():void{
			callServerProxy.unpublish();	
			callServerProxy.hangup();										
			SoundControl.getInstance().stopRingSound();
		}
		
		public function setStatusHold(isHold:Boolean):void{
			if (state == Call.STATE_TALK || state == Call.STATE_HOLD){
				callServerProxy.hold(isHold);
			}
		}		
		
		public function transfer(callee:String):void{
			for each (var tempCall:Call in flash_API.calls){
				if (tempCall.state == Call.STATE_TALK && tempCall.id != id){
					tempCall.setStatusHold(true);
				}
			}
			callServerProxy.transfer(callee);
			SoundControl.getInstance().stopRingSound();
		}		
		
		public function answer(isVideoCall:Boolean = false):void{
			SoundControl.getInstance().stopRingSound();
			for each (var tempCall:Call in flash_API.calls){
				if (tempCall.state == Call.STATE_TALK && tempCall.id != id){
					tempCall.setStatusHold(true);
				}
			}
			callServerProxy.answer(isVideoCall);			
		}
		public function isSendVideo(flag:Boolean):void{
			callServerProxy.isSendVideo(flag);		
		} 
		
		public function sendDTMF(dtmf:String):void{
			callServerProxy.sendDtmf(dtmf);			
		}		
		
		public function publish():void{
			callServerProxy.publish(flash_API.modelLocator.login);
		}
		
		public function unpublish():void{
			callServerProxy.unpublish();
		}		
		
		internal function startTimer():void{
			if (timer!=null){
				timer.removeEventListener(TimerEvent.TIMER,timerHandler);
				timer.stop();
				timer = null;
			}
			timeOfCall = 0;
			timer = new Timer(999);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);					
			timer.start();			
		}
		
		internal function stopTimer():void{
			timeOfCall = 0;
			if (timer != null){
				timer.removeEventListener(TimerEvent.TIMER,timerHandler);
				timer.stop();
				timer = null;
			}
		}

		private function timerHandler(timeEvent:TimerEvent):void{
			timeOfCall++;
		}
		
	}
}